
$(document).ready(function () {
	$('#login').show().animate({   opacity: 1 }, 2000);
	$('.logo').show().animate({   opacity: 1,top: '30%'}, 800,function(){			
		$('.logo').show().delay(1200).animate({   opacity: 1,top: '4%' }, 300,function(){
			$('.formLogin').animate({   opacity: 1,left: '0' }, 300);
			$('.userbox').animate({ opacity: 0 }, 200).hide();
		 });		
	  });
	$(".on_off_checkbox").iphoneStyle();
	$('.tip a ').tipsy({gravity: 'sw'});
	$('.tip input').tipsy({ trigger: 'focus', gravity: 'w' });
});

	    $('.userload').click(function(e){
			$('.formLogin').animate({   opacity: 1,left: '0' }, 300);			    
			  $('.userbox').animate({ opacity: 0 }, 200,function(){
				  $('.userbox').hide();				
			   });
	    });
$("form#formLogin input").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
		loginEvent();        
    }
}); 
function loginEvent() {
	$username = $("#username_id").val();
	$password = $("#password").val();
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	  if($username == "" || $password == "")
	  {
		  showError("Please input username and password!");
		  $('.inner').jrumble({ x: 4,y: 0,rotation: 0 });	
		  $('.inner').trigger('startRumble');
		  setTimeout('$(".inner").trigger("stopRumble")',500);
		  setTimeout('hideTop()',5000);
		  return false;
	  }	
	  else if (!regex.test($username))
		  {
		  showError("Please input email id as username!");
		  $('.inner').jrumble({ x: 4,y: 0,rotation: 0 });	
		  $('.inner').trigger('startRumble');
		  setTimeout('$(".inner").trigger("stopRumble")',500);
		  setTimeout('hideTop()',5000);
		  return false;
		  }
	  else
	  { 
		 hideTop();
		 loading('Checking',1);		
		 setTimeout( "unloading()", 2000 );
		 setTimeout( "Submit()", 2500 );
	  }
}
	$('#but_login').click(function(e){		
		loginEvent();
	});	

		
																 
function Login(){
		$("#login").animate({   opacity: 1,top: '49%' }, 200,function(){
		 $('.userbox').show().animate({ opacity: 1 }, 500);
			$("#login").animate({   opacity: 0,top: '60%' }, 500,function(){
				$(this).fadeOut(200,function(){
				  $(".text_success").slideDown();
				  $("#successLogin").animate({opacity: 1,height: "200px"},500);   			     
				});							  
			 });
     });
	Submit();
}

function Submit() {
	var dataString = 'LoginForm[username]='+ $('#username_id').val() + '&LoginForm[password]=' + $('#password').val() + '&LoginForm[rememberMe]=' + $('#on_off').val();
	$.ajax({  
		  type: "POST",  
		  url: siteUrl+"admin/default/login/",  
		  data: dataString,  
		  success: function() {
			  $("#login").animate({   opacity: 1,top: '49%' }, 200,function(){
					 $('.userbox').show().animate({ opacity: 1 }, 500);
						$("#login").animate({   opacity: 0,top: '60%' }, 500,function(){
							$(this).fadeOut(200,function(){
							  $(".text_success").slideDown();
							  $("#successLogin").animate({opacity: 1,height: "200px"},500);   			     
							});							  
						 });
			     });
			  setTimeout( "window.location.href='"+siteUrl+"admin/dashboard'", 3000 ); 
		  } ,
		  error: function() {
			  showError("Incorrect Username or password!",1000);
			  $('.inner').jrumble({ x: 4,y: 0,rotation: 0 });	
			  $('.inner').trigger('startRumble');
			  setTimeout('$(".inner").trigger("stopRumble")',500);
			  setTimeout('hideTop()',5000);
			  return false;
		  }
		});  
		return false;  
}
	
$('#alertMessage').click(function(){
	hideTop();
});

function showError(str){
	$('#alertMessage').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	
}

function showSuccess(str){
	$('#alertMessage').removeClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
}

function hideTop(){
	$('#alertMessage').animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });	
}	
function loading(name,overlay) {  
	  $('body').append('<div id="overlay"></div><div id="preloader">'+name+'..</div>');
			  if(overlay==1){
				$('#overlay').css('opacity',0.1).fadeIn(function(){  $('#preloader').fadeIn();	});
				return  false;
		 }
	  $('#preloader').fadeIn();	  
 }
 function unloading() {  
		$('#preloader').fadeOut('fast',function(){ $('#overlay').fadeOut(); });
 }